<?php
//zaciatok kontrolovaneho stringu musi zacinat lomitkom
function is_roll($menuroll,$ID){
$index=0;
        while($index<strlen($menuroll))
        {
                $index++;
                if($menuroll[$index]=="/")
                {
                        if($strC==$ID) return true; else $strC="";
                }
                else
                {
                        $strC=$strC.$menuroll[$index];
                }

        }
        return false;
}

function rutine_menu($menuroll,$ID){
if(!is_roll($menuroll,$ID))
    {
    return $menuroll.$ID."/";
    }
    else
    {
    return str_replace("/".$ID."/","/",$menuroll);
    }
}

function DecodeCookie(&$ShopBasket,&$ID,&$Count){
        $index=0;
        $ID="";
        $Count="";
        $strC="";
        while($index<strlen($ShopBasket))
        {
                $index++;
                if($ShopBasket[$index]=="/")
                {
                        $pos        =strpos($strC,"*");
                        $ID        =substr($strC,0,$pos);
                        $Count        =substr($strC,$pos+1,strlen($strC));
                        $a="";
                        $b=strlen($strC)+1;
                        $ShopBasket = substr_replace($ShopBasket,$a,0,$b);
                        return true;
                }
                else
                {
                        $strC=$strC.$ShopBasket[$index];

                }

        }
       return false;
}
function RemoveItemFromCookie(&$ShopBasket,$ID,$Count,$Index){
        $i=0;
        $pos=0;
        while($pos<strlen($ShopBasket))
        {
                if($ShopBasket[$pos]=="/") {
                        $i++;
                        if (($i-1)==$Index) break;
                }
                $pos++;
        }


        $Item="/".$ID."*".$Count."/";
        //$pos =strpos($ShopBasket,$Item);
        $a="/";
        $b=strlen($Item);
        $ShopBasket = substr_replace($ShopBasket,$a,$pos,$b);
}

function PrintProduct($ProductID,$ProductName,$ProductDescr,$ProductPrice,$ProductAvailability,$ProductCode){
  print "<form action=\"shop_basket.php\" target=\"shop_basket\" method=\"post\">";
    print "<table border=0 cellpadding=0 cellspacing=0 width=98%>";
    //zobrazenie nazvu tovaru
    print "<tr><td>";
        print "<table  border=0 cellpadding=0 cellspacing=0 width=100% bgcolor=red><tr>";
          print "<td align=left valign=top>";
            print "<img  src=leftCurve.bmp>";
          print "</td>";
          print "<td align=right>";
            print "<center><b><font color=white>".$ProductName."</font color></b></center>";
          print "</td>";
          print "<td align=right valign=top>";
            print "[".$ProductCode."]";
            print "<img  src=rightCurve.bmp>";
          print "</td>";
          print "</tr></table>";
    print "</td></tr>";

    //zobrazenie obrazka a popisu
    print "<tr><td>";
        $image_name ="admin/img/".$ProductID.".jpg";
        print "<table table border=0 cellpadding=0 cellspacing=0 bgcolor=#FFE1E2 width='100%'><tr><td>";
        print "<tr><td>";
          if (file_exists($image_name)) print "<img  src=\"".$image_name."\" align=\"top\">";
          print($ProductDescr);
          //print "</td></tr>";//???????????????????????????????????
            print "</td></tr></table>";
    print "</td></tr>";

    //zobrazenie informacii
    print "<tr><td>";
        print "<table border=0 cellpadding=0 cellspacing=0 bgcolor=red width=100%>";
        print "<td>cena:<b><font color=white>";
          print $ProductPrice.",-SKK</font color><b>";
          switch($ProductAvailability){
           case 0:print   "<font color=black ><small> ...[nie je na sklade ]</small></font color>";break;
           case 1:print   "<font color=black ><small> ...[Je na sklade ]</small></font color>";break; }
        print "</td>";
        print "<td valign=left width=\"100px\">";
          print "mno�stvo: ";
        print "<input type=\"text\" name=\"Count_product\" value=\"1\" size=\"3\" style=\"height :14px; font-size: 10px; font-family:'Verdana'; border: 0\"/>";
        print "</td>";
        print "<td valign=left width=\"15px\">";
            print "<input type=\"image\" src=\"shop_basket.bmp\" value=\"Submit\" alt=\"VLO�I� DO KO��KA\">";
        print "</td>";
        print "</table>";
    print "</td></tr>";

    print "</table>";
    print "<input type=\"hidden\" name=\"ID_product\" value=\"".$ProductID."\">";


  print "</form>";
}
function RoundIt($FloatNumber){                     //1,234
    return number_format(round($FloatNumber,2), 2, ',', ' ');
}
/*
function RoundItSKK($FloatNumber){                     //1,234
    $RoughRound     = round($FloatNumber,2);        //1,23
    $FloatElement   = fmod($RoughRound,1);          //0,23
    $IntElement     = $RoughRound-$FloatElement;    //1
    if (($FloatElement>=0) && ($FloatElement<0.25))             //0 az 0.24 zaokruhluje dolu
        return number_format($IntElement, 2, ',', ' ');

    if (($FloatElement>=0.25) && ($FloatElement<0.75))          //0.25 az 0,74 zaokruhluje na 0.5
        return number_format(($IntElement+0.5), 2, ',', ' ');

    if (($FloatElement>=0.75) && ($FloatElement<=0.99))         //0.75 az 0.99 zaokr. cele hore
        return number_format(($IntElement+1), 2, ',', ' ');
}
 */
function ShortIt($FullString,$MaxLength){
          if (strlen($FullString)>$MaxLength)
                  return substr($FullString,0,$MaxLength)."...";
          else
                  return $FullString;
}
function FormatItAsCurrency($FloatNumber){
          return number_format($FloatNumber, 2, ',', ' ');
}
function FormatItAsCurrencyTEXTEDIT($FloatNumber){
          return number_format($FloatNumber, 2, '.', '');
}
function PrintLine(){
    //print "<table border=1 cellpadding=0 cellspacing=0 width=\"100%\">";
    //print "<tr><td></td></tr>";
    //print "<table>";
    print "<hr width=98% align=left style=\"border-top:1px solid black; margin-bottom=0; margin-top=0;\">";
}

function ProductIsReadyForExpedition($P_ID){
    if(!is_numeric($P_ID) or $P_ID<1) return false;
    $sql_result = mysql_query('SELECT OP_Ready FROM order_products WHERE ID='.$P_ID);
    $sql_object = mysql_fetch_object($sql_result);

    if($sql_object->OP_Ready == '1') return true;
    else return false;
}

function Change_Product_Ready_Status($P_ID){
    if(ProductIsReadyForExpedition($P_ID)){
        mysql_query('UPDATE order_products SET OP_Ready="0" WHERE ID='.$P_ID);
    }
    else{
        mysql_query('UPDATE order_products SET OP_Ready="1" WHERE ID='.$P_ID);
    }
}
function Get_Order_NO($OID){
    $sql_result = mysql_query('SELECT ID, NO, Order_Create FROM order_heads WHERE ID='.$OID);
    $sql_obj = mysql_fetch_object($sql_result);

    if($sql_obj->NO == '') $OrderNO = $sql_obj->ID;
    else $OrderNO = date('Y',strtotime($sql_obj->Order_Create)).'/'.$sql_obj->NO;

    return $OrderNO;
}
?>